package com.enjoy.mjbaseframe.util;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.concurrent.ExecutionException;

import static com.enjoy.mjbaseframe.constant.BaseVariable.DEFAULT_RESID;


/**
 * Glide图片处理工具类
 * Created by Administrator on 2017/12/28.
 */

public class GlideUtil {

    public static final int GLIDE_SUCCESS = 1;
    public static final int GLIDE_FAILURE = 2;

    public static RequestOptions  getRequest(Context mContext) {
        return getRequest(mContext,DEFAULT_RESID);
    }

    public static RequestOptions  getRequest(Context mContext,int resId) {
        final RequestOptions RECYCLE_OPTIONS=new RequestOptions().fitCenter()
                .placeholder(mContext.getResources().getDrawable(DEFAULT_RESID)) //4.0之后放这里了
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate();
        return RECYCLE_OPTIONS;
    }

    /**
     * Glide加载图片
     * @param mContext
     * @param url
     * @param mImg
     * @param resId
     */
    public static void loadImageView(Context mContext,String url,ImageView mImg,int resId){
        Glide.with(mContext).load(url).apply(getRequest(mContext,resId)).into(mImg);
    }
    public static void loadImageView(Context mContext,String url,ImageView mImg){
        Glide.with(mContext).load(url).apply(getRequest(mContext)).into(mImg);
    }

    /**
     * Glide 获得图片缓存路径
     */
    private String getImagePath(Context context,String imgUrl) {
        String path = null;
        FutureTarget<File> future = Glide.with(context)
                .downloadOnly()
                .load(imgUrl)
                .submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
        try {
            File cacheFile = future.get();
            path = cacheFile.getAbsolutePath();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return path;
    }


    /**
     * Glide获取缓存图片路径，需要开启子线程
     * @param context
     * @param url
     */
    public static void getPathByGlide(final Context context, final String url,final Handler handler){
        new Thread(new Runnable() {
            @Override
            public void run() {
                File imgFile = null;
                FutureTarget<File> target = Glide.with(context)
                        .asFile()
                        .load(url)
                        .submit();
                try {
                    imgFile = target.get();
                    Logger.d("imgFile:" + imgFile.getAbsolutePath());
                    if (imgFile != null && FileUtil.fileIsExists(imgFile.getAbsolutePath())) {
                        Message msg = Message.obtain();
                        msg.what = GLIDE_SUCCESS;
                        msg.obj = imgFile.getAbsolutePath();
                        handler.sendMessage(msg);
                    }else {
                        handler.sendEmptyMessage(GLIDE_FAILURE);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
